package com.itheima.tliaswebmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tliaswebmanagement.exception.NamesAreIrregularException;
import com.itheima.tliaswebmanagement.mapper.EmpExprMapper;
import com.itheima.tliaswebmanagement.mapper.EmpMapper;
import com.itheima.tliaswebmanagement.pojo.common.PageResult;
import com.itheima.tliaswebmanagement.pojo.dto.EmpDTO;
import com.itheima.tliaswebmanagement.pojo.dto.EmpPageQueryDTO;
import com.itheima.tliaswebmanagement.pojo.entity.Emp;
import com.itheima.tliaswebmanagement.pojo.entity.EmpExpr;
import com.itheima.tliaswebmanagement.pojo.entity.EmpLog;
import com.itheima.tliaswebmanagement.pojo.vo.EmpVO;
import com.itheima.tliaswebmanagement.pojo.vo.LoginInfo;
import com.itheima.tliaswebmanagement.service.EmpLogService;
import com.itheima.tliaswebmanagement.service.EmpService;
import com.itheima.tliaswebmanagement.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @Description EmpServiceImpl
 * @Author ziguan ling
 * @Date 2025--01--06
 */

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult<EmpVO> page(EmpPageQueryDTO empPageQueryDTO) {
//        Integer start = (empPageQueryDTO.getPage() - 1) * empPageQueryDTO.getPageSize();
//        empPageQueryDTO.setPage(start);
        PageHelper.startPage(empPageQueryDTO.getPage(), empPageQueryDTO.getPageSize());

//        List<EmpVO> rows = empMapper.pageQuery(empPageQueryDTO);
        Page<EmpVO> page = (Page<EmpVO>) empMapper.pageQuery(empPageQueryDTO);

//        Long total = empMapper.count(empPageQueryDTO);

//        PageResult<EmpVO> PageResult = new PageResult<>(total, rows);
        PageResult<EmpVO> PageResult = new PageResult<>(page.getTotal(), page.getResult());

        return PageResult;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void addEmp(EmpDTO empDTO) {
        if (empDTO.getName().contains("老王")) {
            throw new NamesAreIrregularException("这个名字太坏了,请重新设定");
        }

//添加员工信息
        //spring框架实现：开启事务:begin
        try {
            Emp emp = new Emp();
            BeanUtils.copyProperties(empDTO, emp);
            log.info("添加员工信息:" + emp);

            emp.setCreateTime(null);
            emp.setUpdateTime(null);

            empMapper.insertEmp(emp);

            // 批量保存员工工作经历
            List<EmpExpr> exprList = empDTO.getExprList();
            if (exprList != null && exprList.size() > 0) {

                exprList.forEach(expr -> {
                    expr.setEmpId(emp.getId());
                });

                empExprMapper.insertEmpExprBatch(exprList);
            }

        } finally {
            //3.记录添加日志
            EmpLog empLog = new EmpLog();
            empLog.setInfo("新增员工");
            empLog.setOperateTime(LocalDateTime.now());
            empLogService.insertLog(empLog);
        }
    }





    @Override
    public LoginInfo getLoginInfo(Emp emp) {


        Emp e = empMapper.getLoginInfo_d(emp);

        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id", 100);
        claims.put("username", "admin");

        String token = JwtUtils.generateJwt(claims);

        if (e != null) {
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setId(e.getId());
            loginInfo.setUsername(e.getUsername());
            loginInfo.setName(e.getName());
            loginInfo.setToken(token);

            return loginInfo;
        }
        return null;
    }

    @Override
    public EmpVO queryById_s(Integer id) {

        log.info("根据id:{}查询员工信息和员工工作经历",id);

        EmpVO empVO = empMapper.queryById_d(id);

        return empVO ;
    }

    @Override
    public List<Emp> queryAll_s() {
        List<Emp> list = empMapper.queryAll_d();

        return list;

    }

    @Transactional  //事务管理
    @Override
    public void update_s(EmpDTO empDTO) {
        Emp updateEmp = new Emp();
        BeanUtils.copyProperties(empDTO, updateEmp);
        updateEmp.setUpdateTime(LocalDateTime.now());
        empMapper.update_d(updateEmp);

        empExprMapper.deleteByEmpId(empDTO.getId());

        List<EmpExpr> exprList = empDTO.getExprList();

        if(exprList != null && exprList.size() > 0){
            //把前面更新的员工id绑定给当前的员工经历
            exprList.forEach(expr -> expr.setEmpId(empDTO.getId()));
            empExprMapper.insertEmpExprBatch(exprList);
        }

    }

    @Transactional
    @Override
    public void removeBatch_s(List<Integer> ids) {
        log.info("删除员工:{}",ids);
        empMapper.removeBatch_d(ids);
        empExprMapper.removeBatch_d2(ids);
    }
}































